﻿namespace apitest.Application
{
    public static class DateHelper
    {

        public static int GetWeekOfYear(DateTime dt)
        {

            System.Globalization.GregorianCalendar gc = new System.Globalization.GregorianCalendar();
            int weekOfYear = gc.GetWeekOfYear(dt, System.Globalization.CalendarWeekRule.FirstDay, DayOfWeek.Monday);
            return weekOfYear;
        }


        public static int GetWeekOfMonth(DateTime day, int WeekStart = 1)
        {
            //WeekStart
            //1表示 周一至周日 为一周
            //2表示 周日至周六 为一周
            DateTime FirstofMonth;
            FirstofMonth = System.Convert.ToDateTime(day.Date.Year + "-" + day.Date.Month + "-" + 1);

            int i = (int)FirstofMonth.Date.DayOfWeek;
            if (i == 0)
            {
                i = 7;
            }

            if (WeekStart == 1)
            {
                return (day.Date.Day + i - 2) / 7 + 1;
            }
            if (WeekStart == 2)
            {
                return (day.Date.Day + i - 1) / 7;

            }
            return 0;
            //错误返回值0
        }

        /// <summary>
        /// 计算时间差
        /// </summary>
        /// <param name="start"></param>
        /// <param name="end"></param>
        /// <param name="type">1=秒，2=分，3=时，4= 天</param>
        /// <returns></returns>
        public static int GetDateDiff(DateTime start, DateTime end, int type)
        {
            TimeSpan ts = end - start;
            switch (type)
            {
                case 1:
                    return (int)ts.TotalSeconds;
                case 2:
                    return (int)ts.TotalMinutes;
                case 3:
                    return (int)ts.TotalHours;
                case 4:
                    return (int)ts.TotalDays;
                default:
                    throw new Exception("类型不正确");
            }
        }

        public static DateRange GetDateRange(string type)
        {
            type = type.ToLower();
            var now = DateTime.Now;
            DateTime start;
            DateTime end;
            switch (type)
            {
                case "day":
                    start = now.Date;
                    end = start.AddDays(1).AddSeconds(-1);
                    break;
                case "week":
                    start = now.Date.AddDays(1 - int.Parse(now.DayOfWeek.ToString("d"))); ;
                    end = start.AddDays(7).AddSeconds(-1);
                    break;
                case "month":
                    start = now.AddDays(1 - now.Day).Date;
                    end = start.AddMonths(1).AddSeconds(-1); ;
                    break;
                case "year":
                    start = new DateTime(now.Year, 1, 1); ;
                    end = new DateTime(now.Year, 12, 31).AddDays(1).AddSeconds(-1); ;
                    break;
                default:
                    throw new Exception("时间类型错误");
                    break;

            }
            return new DateRange()
            {
                Start = start,
                End = end
            };
        }

        public static List<Dictionary<string, DateTime>> GetMonthOfYear(string year)
        {
            var list = new List<Dictionary<string, DateTime>>();
            var yearStart = DateTime.Parse($"{year}-01-01");
            list.Add(new Dictionary<string, DateTime>()
            {
                {"start",yearStart},
                {"end",yearStart.AddMonths(1).AddSeconds(-1)},
            });
            for (int i = 1; i < 12; i++)
            {
                var last = list.LastOrDefault();
                var dic = new Dictionary<string, DateTime>()
            {
                {"start",last["start"].AddMonths(1)},
                {"end",last["start"].AddMonths(2).AddSeconds(-1)},
            };
                list.Add(dic);
            }
            return list;
        }
    }



    public class DateRange
    {
        public DateTime Start { get; set; }
        public DateTime End { get; set; }
    }


}
